const tableHead = {
    "geneid": {"title": "Gene ID", "searchType": "text", "formatter": "GetGeneId"},
    "chr": {"title": "Chromsome", "searchType": "text", "formatter": ""},
    "start": {"title": "Strat Position", "searchType": "num", "formatter": ""},
    "end": {"title": "End Position", "searchType": "num", "formatter": ""},
    "strand": {"title": "Strand", "searchType": "text", "formatter": ""},
    "go": {"title": "GO", "searchType": "text", "formatter": "GetGo"},
    "pfam": {"title": "Pfam ID", "searchType": "text", "formatter": "GetPfam"},
    "pather": {"title": "PANTHER ID", "searchType": "text", "formatter": "GetPanther"},
    "interpro": {"title": "Interpro ID", "searchType": "text", "formatter": "GetInterpro"}
};

function getTableColumns(headJson, table) {
    let columns = [];
    let checkbox = "";
    $.each(headJson, (field, json) => {
        let column = {
            field: field,
            title: json.title,
            sortable: true,
            searchType: json.searchType
        }

        if (json.searchType === "checkbox") {
            column["searchSelect"] = json.searchSelect
        }

        if (json.formatter !== "") {
            column["formatter"] = json.formatter
        }

        columns.push(column)
        checkbox += `<div class="form-check"><label class="form-check-label">
                            <input type='checkbox' checked='checked' value="${field}"  onclick="setColumns('${field}')" id="${field}"> ${json.title}
                           <i class="input-helper"></i> </label></div>`
    })
    $(`#${table}-check`).html(checkbox)
    return columns;
}

function GetGeneId(value, row) {
    return `<a href="/hordeum/browse/gene/${row.id}">${value}</a>`
}

function GetGo(value) {
    return value.split(";").map(x => {
        return `<a href="http://amigo.geneontology.org/amigo/term/${x}" target="_blank">${x}</a>`
    }).join(" ")
}

function GetPfam(value) {
    return value.split(";").map(x => {
        return `<a href="http://pfam.xfam.org/family/${x}" target="_blank">${x}</a>`
    }).join(" ")
}

function GetPanther(value) {
    return value.split(";").map(x => {
        return `<a href="http://www.pantherdb.org/panther/family.do?clsAccession=${x}" target="_blank">${x}</a>`
    }).join(" ")
}

function GetInterpro(value) {
    return value.split(";").map(x => {
        let interpro = x.split(":")[0]
        return `<a href="http://www.ebi.ac.uk/interpro/entry/InterPro/${interpro}" target="_blank">${interpro}</a>`
    }).join(" ")
}

function initTable() {
    const columns = getTableColumns(tableHead, "table")
    $("#table").bootstrapTable({columns: columns})
}